<template>
  <div class="container">
    <!-- 左侧目录 -->
    <nav class="sidebar">
      <div class="chapter" v-for="chapter in chapters" :key="chapter.id">
        <div class="chapter-header" @click="toggleExpand(chapter)">
          <span class="collapse-icon">▶</span>
          <span class="chapter-title" :class="{ active: chapter.id === activeChapter }">
            {{ chapter.title }}
          </span>
        </div>

        <div class="sub-chapters" v-if="chapter.expanded">
          <div
            class="sub-chapter"
            v-for="sub in chapter.subChapters"
            :key="sub.id"
            @click="showDetail(sub)"
            @mousemove="handleMouseMove(sub, $event)"
            @mouseleave="resetStyle(sub)"
            :class="{
              active: sub.id === activeChapter,
              hover: sub.hover
            }"
            :style="sub.hoverStyle"
          >
            <span class="bullet">•</span>
            <span class="sub-title">{{ sub.title }}</span>
          </div>
        </div>
      </div>
    </nav>

    <!-- 右侧正文区域 -->
    <main class="content">
      <div v-html="contentHtml"></div>
      <div class="detail-modal" v-if="selectedSub">
        <h3>{{ selectedSub.title }}</h3>
        <p>{{ selectedSub.detail }}</p>
        <button @click="closeDetail">关闭</button>
      </div>
    </main>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chapters: [
        {
          id: 1,
          title: '第一章 技术方案',
          expanded: true,
          subChapters: [
            {
              id: 11,
              title: '第一节 系统配置与功能概述',
              detail: '系统配置包括硬件组件、软件环境...'
            },
            {
              id: 12,
              title: '第二节 技术选型与架构',
              detail: '技术选型采用React+Node.js...'
            }
          ]
        }
      ],
      activeChapter: null,
      contentHtml: '',
      selectedSub: null,
      hoverTimeout: null
    };
  },
  methods: {
    toggleExpand(chapter) {
      chapter.expanded = !chapter.expanded;
    },
    showDetail(sub) {
      this.selectedSub = sub;
    },
    closeDetail() {
      this.selectedSub = null;
    },
    handleMouseMove(sub, event) {
      clearTimeout(this.hoverTimeout);
      const rect = event.target.getBoundingClientRect();
      const yOffset = event.clientY - rect.top;

      sub.hoverStyle = {
        transform: `translateY(-${yOffset * 0.2}px)`,
        boxShadow: `0 ${yOffset * 0.1}px 8px rgba(0,0,0,0.1)`
      };
    },
    resetStyle(sub) {
      this.hoverTimeout = setTimeout(() => {
        sub.hoverStyle = {};
      }, 200);
    },
    async fetchContent() {
      try {
        // const response = await axios.get('your-api-endpoint');
        // this.contentHtml = response.data;
      } catch (error) {
        console.error('获取内容失败:', error);
      }
    }
  },
  mounted() {
    this.fetchContent();
  }
};
</script>

<style scoped>
.container {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: 300px;
  padding: 20px;
  background: white;
  border-right: 1px solid #eee;
  overflow-y: auto;
}

.chapter-header {
  display: flex;
  align-items: center;
  padding: 10px;
  cursor: pointer;
  transition: background 0.2s;
}

.chapter-header:hover {
  background: #f5f5f5;
}

.collapse-icon {
  margin-right: 10px;
  transition: transform 0.2s;
}

.chapter.expanded .collapse-icon {
  transform: rotate(90deg);
}

.chapter-title {
  font-weight: bold;
}

.sub-chapters {
  margin-left: 20px;
  border-left: 2px solid #eee;
  padding-left: 10px;
}

.sub-chapter {
  padding: 8px 0;
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
  z-index: 1;
}

.sub-chapter:hover {
  background: #f8f8f8;
}

.sub-chapter.active {
  font-weight: bold;
  color: #2196f3;
}

.sub-chapter.hover {
  transition:
    transform 0.2s,
    box-shadow 0.2s;
}

.bullet {
  margin-right: 8px;
}

.content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background: white;
  position: relative;
}

.detail-modal {
  position: absolute;
  top: 20px;
  right: 20px;
  background: white;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

.active {
  font-weight: bold;
  color: #2196f3;
}
</style>
